package com.camel.docker.entity.search;

import com.alibaba.fastjson.JSONArray;
import com.camel.docker.entity.Project;
import com.camel.docker.entity.docker.*;
import com.github.pagehelper.util.StringUtil;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
public class ContainerAddForm implements Serializable {
    private Integer id;
    // 镜像名称
    private String name;

    // 内外端口
    private List<PortMap> ports;

    // 环境变量
    private List<Env> envs;

    // 镜像
    private String image;

    // 启动信息
    private StartInfo startInfo;

    // 启动指令
    private String directive;

    // 挂载卷
    private List<Volume> volumes;

    // 工作目录
    private String workDir;

    public Project loadContainer() {
        Project container = new Project();
        container.setId(this.id);
        container.setName(this.name);
        container.setPorts(this.ports.toString());
        container.setEnvs(this.envs.toString());
        container.setImage(this.image);
        container.setStartType(this.startInfo.getStartType());
        container.setAddr(this.startInfo.getAddr());
        container.setDirective(this.getDirective());
        container.setVolumes(this.volumes.toString());
        container.setWorkDir(this.workDir);
        return container;
    }

    public ContainerAddForm() {
    }

    public ContainerAddForm(Project container) {
        this.setName(container.getName());
        this.setPorts(StringUtil.isNotEmpty(container.getPorts()) ? JSONArray.parseArray(container.getPorts(), PortMap.class) : new ArrayList<>());
        this.setEnvs(StringUtil.isNotEmpty(container.getEnvs()) ? JSONArray.parseArray(container.getEnvs(), Env.class) : new ArrayList<>());
        this.setImage(container.getImage());
        StartInfo startInfo = new StartInfo(container.getStartType(), container.getAddr());
        this.setStartInfo(startInfo);
        this.setVolumes(StringUtil.isNotEmpty(container.getVolumes()) ? JSONArray.parseArray(container.getVolumes(), Volume.class) : new ArrayList<>());
        this.setWorkDir(container.getWorkDir());
        this.setDirective(container.getDirective());
    }
}
